﻿using SharpDX.Direct2D1;
using SharpDX.DXGI;
using SharpDX;

using AlphaMode = SharpDX.Direct2D1.AlphaMode;
using Factory = SharpDX.Direct2D1.Factory;

namespace Ensemble_Windows
{
    /// <summary>
    /// Direct2D11
    /// </summary>
    public class Direct2D1Render : Direct3D11Render
    {
        /// <summary>
        /// Factory2D
        /// </summary>
        public Factory Factory2D { get; private set; }
        /// <summary>
        /// FactoryDWrite
        /// </summary>
        public SharpDX.DirectWrite.Factory FactoryDWrite { get; private set; }
        /// <summary>
        /// 2D渲染目标
        /// </summary>
        public RenderTarget RenderTarget2D { get; private set; }
        /// <summary>
        /// 场景颜色画刷
        /// </summary>
        public SolidColorBrush SceneColorBrush { get; private set; }

        /// <summary>
        /// 游戏初始化
        /// </summary>
        /// <param name="configuration">配置</param>
        protected override void Initialize(Configuration configuration)
        {
            base.Initialize(configuration);
            Factory2D = new SharpDX.Direct2D1.Factory();
            using (var surface = BackBuffer.QueryInterface<Surface>())
            {
                RenderTarget2D = new RenderTarget(Factory2D, surface,
                    new RenderTargetProperties(
                        new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));
            }
            RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;

            FactoryDWrite = new SharpDX.DirectWrite.Factory();

            SceneColorBrush = new SolidColorBrush(RenderTarget2D, Color.White);
        }

        /// <summary>
        /// 开始绘图
        /// </summary>
        protected override void BeginDraw()
        {
            base.BeginDraw();
            RenderTarget2D.BeginDraw();
        }

        /// <summary>
        /// 结束绘图
        /// </summary>
        protected override void EndDraw()
        {
            RenderTarget2D.EndDraw();
            base.EndDraw();
        }
    }
}
